package com.xunk.system.service.impl;

import com.xunk.common.core.domain.MandunFacility;
import com.xunk.common.core.enums.DeviceEnum;
import com.xunk.common.core.utils.DateUtils;
import com.xunk.common.core.utils.StringUtils;
import com.xunk.common.core.utils.serialization.JsonSerializer;
import com.xunk.common.core.web.domain.AjaxResult;
import com.xunk.system.domain.SysDevice;
import com.xunk.system.feign.EquipmentClient;
import com.xunk.system.mapper.SysDeviceMapper;
import com.xunk.system.service.ISysDeviceService;
import com.xunk.system.vo.RoomDeviceInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ProjectName: xunk
 * @Package: com.xunk.buz.service.impl
 * @ClassName: MandunDeviceServiceImpl
 * @Author: luanzhiwei
 * @Description: 曼顿设备service业务层处理
 * @Date: 2021/3/30 9:13
 * @Version: 1.0
 */
@Service
public class MandunDeviceServiceImpl implements ISysDeviceService {

    @Autowired
    private SysDeviceMapper sysDeviceMapper;

    @Autowired
    private EquipmentClient equipmentClient;

    /**
     * 查询设备信息，默认前装人员安装设备时，已经添加了家庭信息等可选的信息，但未添加设备硬件信息等。
     * 如果不存在设备的硬件信息，则查询曼顿接口，并保存设备硬件信息
     *
     * @param deviceSn 设备ID
     * @return 设备
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult selectSysDeviceById(String deviceSn) {
        SysDevice sysDevice = sysDeviceMapper.selectSysDeviceById(deviceSn);
        //不存在设备，添加设备
        if (ObjectUtils.isEmpty(sysDevice)) {
            AjaxResult ajaxResult = equipmentClient.getBox(String.valueOf(deviceSn));
            if (!ajaxResult.isSuccess()) {
                return AjaxResult.error(ajaxResult.getMsg());
            }
            sysDevice = this.mandun2Device(new SysDevice(), ajaxResult.getData());
            this.insertSysDevice(sysDevice);
        } else if (StringUtils.isEmpty(sysDevice.getSupplier())) {
            //曼顿保存设备设备厂商只在这里进行，如果厂商为空，表示该设备只录入了设备号，未从曼顿查询并保存其他信息。
            AjaxResult resVO = equipmentClient.getBox(String.valueOf(deviceSn));
            sysDevice = this.mandun2Device(sysDevice, resVO.getData());
            this.updateSysDevice(sysDevice);
        }

        return AjaxResult.success(sysDevice);
    }

    /**
     * 查询设备信息
     *
     * @param sysDevice 设备
     * @return
     */
    @Override
    public List<SysDevice> selectSysDeviceList(SysDevice sysDevice) {
        List<SysDevice> sysDevices = sysDeviceMapper.selectSysDeviceList(sysDevice);
        return sysDevices.stream().peek(item -> {
            String deviceType = item.getDeviceType();
            DeviceEnum deviceEnum = DeviceEnum.parseDeviceEnum(deviceType);
            item.setDeviceTypeName(deviceEnum.getDeviceName());
        }).collect(Collectors.toList());
    }

    /**
     * 保存设备信息
     *
     * @param sysDevice 设备
     * @return
     */
    @Override
    public int insertSysDevice(SysDevice sysDevice) {
        sysDevice.setCreateTime(DateUtils.getNowDate());
        AjaxResult resVO = equipmentClient.getBox(String.valueOf(sysDevice.getDeviceSn()));
        if (!resVO.isSuccess()) {
            return 0;
        }
        sysDevice = this.mandun2Device(new SysDevice(), resVO.getData());
        int i = sysDeviceMapper.insertSysDevice(sysDevice);
        return i;
    }

    /**
     * 更新设备信息
     *
     * @param sysDevice 设备
     * @return
     */
    @Override
    public int updateSysDevice(SysDevice sysDevice) {
        return sysDeviceMapper.updateSysDevice(sysDevice);
    }

    /**
     * 批量删除设备
     *
     * @param deviceSns 需要删除的设备ID
     * @return
     */
    @Override
    public int deleteSysDeviceByIds(String[] deviceSns) {
        return sysDeviceMapper.deleteSysDeviceByIds(deviceSns);
    }

    /**
     * 删除设备信息
     *
     * @param deviceSn 设备ID
     * @return
     */
    @Override
    public int deleteSysDeviceById(String deviceSn) {
        return sysDeviceMapper.deleteSysDeviceById(deviceSn);
    }

    private SysDevice mandun2Device(SysDevice device, Object facility) {
        MandunFacility deserialize = JsonSerializer.deserialize(JsonSerializer.serialize(facility), MandunFacility.class);
        device.setDeviceSn(deserialize.getMac());
        if (StringUtils.isEmpty(device.getDeviceName())) {
            device.setDeviceName(deserialize.getAliasName());
        }
        if (StringUtils.isEmpty(device.getDeviceType())) {
            device.setDeviceType("airSwitch");
        }
        if (StringUtils.isEmpty(device.getManufacturer())) {
            device.setManufacturer("mandun");
        }
        if (StringUtils.isEmpty(device.getSupplier())) {
            device.setSupplier("mandun");
        }
        if (StringUtils.isEmpty(device.getBrand())) {
            device.setBrand("曼顿");
        }
        if (ObjectUtils.isEmpty(device.getDeviceSource())) {
            device.setDeviceSource(1);
        }
        device.setDeviceOnline(deserialize.getOnline() == 1 ? 1L : 0L);
        device.setSupplier(deserialize.getProjectCode());
        return device;
    }
}
